#!/usr/bin/python
# coding:utf-8
import urllib
import ssl
import re
import time
import sqlite3
import os
import _thread
import sys
import atexit
import re
from urllib.parse import quote
from urllib.request import urlopen

def get_video(videopage):
    r_v=r'"quality":".*?","videoUrl":"(.*?)"}'
    r_cover=r'"image_url":"(.*?)",'
    r_title=r'data-video-title="(.*?)"'
    r_quality=r'quality":"(\d*?)","videoUrl":".*?"}'
    r_duration=r'"video_duration":"(\d.*?)"'
    r_view=r'<div class="views"><span class="count">(.*?)</span>'
    r_percent=r'<span class="percent">(.*?)</span>'
    r_up=r'<span class="votesUp">(.*?)</span>'
    r_down=r'<span class="votesDown">(.*?)</span>'
    r_cate=r'<a href="/video.*?" onclick="ga.*?;">(.*?)</a>'

    l_v=list(map(lambda x:x.replace('\\',''),re.findall(r_v,videopage)))
    l_q=re.findall(r_quality,videopage)

    adds={q:v.replace('\\','') for q,v in zip(l_q,l_v)}
    duration=int(re.findall(r_duration,videopage)[0])
    title=re.findall(r_title,videopage)[0]
    infos={'duration':duration,'cover':re.findall(r_cover,videopage)[0].replace('\\',''),'views':re.findall(r_view,videopage)[0],'percent':re.findall(r_percent,videopage)[0],'up':re.findall(r_up,videopage)[0],'down':re.findall(r_down,videopage)[0],'categories':re.findall(r_cate,videopage)}

    return (adds,title,infos)

def show_infos(adds,title,infos):
    text='Video title:{}\nDuration:{}\nAvailable qualities:{}\nCategories:{}'
    cats=','.join(infos['categories'])
    duration=infos['duration']
    qualities='\n'.join([str(q)+' : '+str(url) for q,url in adds.items()])
    text=text.format(title,duration,qualities,cats)
    print(text)

def daemonize(pid_file=None,work_dir='/'):
    pid = os.fork()
    if pid:
        sys.exit(0)
    os.chdir(work_dir)
    os.umask(0)
    os.setsid()
    _pid = os.fork()
    if _pid:
        sys.exit(0)
    sys.stdout.flush()
    sys.stderr.flush()
    with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
        os.dup2(read_null.fileno(), sys.stdin.fileno())
        os.dup2(write_null.fileno(), sys.stdout.fileno())
        os.dup2(write_null.fileno(), sys.stderr.fileno())
    if pid_file:
        with open(pid_file, 'w+') as f:
            f.write(str(os.getpid()))
        atexit.register(os.remove, pid_file)

def getHtml(url):
    try:
        context = ssl._create_unverified_context()
        header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:48.0) Gecko/20100101 Firefox/48.0"}
        response = urlopen(url)
        text = response.read()
        return text.decode("utf-8")
    except BaseException as arg:
        print(arg)
        return "error"


def getPornPage(keyword, page):
    url = "http://www.pornhub.com/video/search?"
    textmod = "search="+ keyword.decode("utf-8") +"&page="+ str(page)
    #textmod = quote(textmod.encode("utf-8"), safe='/', encoding=None, errors=None)
    url = url + textmod
    pornPage = getHtml(url)
    if pornPage == "error":
        while pornPage == "error":
            print("get Page time out")
            time.sleep(3)
            pornPage = getHtml(url)
    return pornPage

def getPornList(html):
    pat = re.compile(
        r'<a href="/view_video\.php\?viewkey=[\s\S]{0,120}title="')#class="img" data-related-url="[\s\S]{0,300}data-mediumthumb="[\s\S]{0,120}\.jpg"')
    list = pat.findall(html)
    #print(list)
    return list

def getPornTitle(listItem):
    pat = re.compile(r'title=".* class="img"')
    title = pat.findall(listItem)
    endPos = len(title[0]) - 13
    return title[0][7:endPos]

def getPornUrl(listItem):
    pat = re.compile(r'<a href=".*title="')
    url = pat.findall(listItem)
    endPos = len(url[0]) - 9
    return "www.pornhub.com" + url[0][9:endPos]

def getPornImgUrl(listItem):
    pat = re.compile(r'data-mediumthumb=".{0,150}\.jpg"')
    url = pat.findall(listItem)
    endPos = len(url[0]) - 1
    return url[0][18:endPos]

def insertDb(listItem, conn):
    #title = getPornTitle(listItem)
    url = getPornUrl(listItem)
    #img = getPornImgUrl(listItem)
    img = ""
    hurl = "https://"+url
    print(hurl)
    videopage = getHtml(hurl)
    if videopage == "error" :
      while videopage == "error" :
        time.sleep(3)
        videopage = getHtml(hurl)
    try:
      adds,title,infos = get_video(videopage)
      _time = str(int(time.time()))
      titles = conn.cursor().execute("SELECT TITLE from VIDEOS")
      if (not '240' in adds) or (adds["240"] == "") :
        adds['240'] = "none"
      if (not '480' in adds) or (adds["480"] == "") :
        adds['480'] = "none"
      if (not '720' in adds) or (adds["720"] == "") :
        adds['720'] = "none"
      if (not '1080' in adds) or (adds["1080"] == "") :
        adds['1080'] = "none"
      for row in titles:
          if row[0].encode("utf-8") == title:
              return
      print(title)
      print(adds)
      sql = "INSERT OR IGNORE INTO VIDEOS(TITLE,URL,IMG,TIME,Dlow,Dmid,Dhig,Dfan) VALUES ('" + title + "','" + hurl + "','" + img + "','" + _time + "','"+adds['240']+"','"+adds['480']+"','"+adds['720']+"','"+adds['1080']+"')"
      conn.cursor().execute(sql);
    except BaseException as arg:
        print(arg)


def getPorn(keyword, pages, conn):
    for page in range(1, pages + 1):
        print("getting about " + keyword.decode("utf-8") + " page " + str(page) + '\n')
        pornPage = getPornPage(keyword, page)
        pornList = getPornList(pornPage)
        limit = 0
        for item in pornList:
            insertDb(item, conn)
            limit = limit + 1
            if limit == 2:
                pass
                #    return


def getPornWithKeywords(conn):
    words = conn.cursor().execute("SELECT * from KEYWORD order by rowid desc")
    for keys in words:
        getPorn(keys[0].encode("utf-8"), 1, conn)

def autoReflesh():
    conn = sqlite3.connect('porn.db')
    c = conn.cursor()
    c.execute('''
    	CREATE TABLE IF NOT EXISTS VIDEOS
       (TITLE   TEXT,
        URL     TEXT,
        IMG     TEXT,
        TIME    INTEGER,
        Dlow    TEXT,
        Dmid    TEXT,
        Dhig    TEXT,
        Dfan   TEXT);
    ''')
    c.execute('''
    	CREATE TABLE IF NOT EXISTS KEYWORD
       (WORDS   TEXT);
    ''')
    getPornWithKeywords(conn)
    conn.commit()
    print("reflesh!")
    time.sleep(2)
    conn.close()

def addKeyword(word):
    keyword = word.encode("utf-8")
    conn = sqlite3.connect('porn.db')
    c = conn.cursor()
    c.execute('''
        CREATE TABLE IF NOT EXISTS KEYWORD
        (WORDS   TEXT);
    ''')
    words = c.execute("SELECT WORDS FROM KEYWORD")
    for word in words:
        if word[0].encode("utf-8") == keyword:
            return
    sql = "INSERT OR IGNORE INTO KEYWORD (WORDS) VALUES ('" + keyword.decode("utf-8") + "')"
    c.execute(sql);
    conn.commit()
    conn.close()


if __name__ == '__main__':
    #work_dir="/root/phpython"
    try:
        #daemonize(work_dir+"/daemonize.log",work_dir)
        addKeyword("teen");
        autoReflesh()
    except BaseException as arg:
        print(arg)

